package com.sevattal.service.leaves.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sevattal.common.base.result.ResultCodeEnum;
import com.sevattal.common.base.util.JwtInfo;
import com.sevattal.common.base.util.JwtUtils;
import com.sevattal.common.base.util.MD5;
import com.sevattal.service.base.exception.SevattalException;
import com.sevattal.service.leaves.entity.Dept;
import com.sevattal.service.leaves.entity.Emp;
import com.sevattal.service.leaves.entity.EmpAuthority;
import com.sevattal.service.leaves.entity.vo.DeptVo;
import com.sevattal.service.leaves.entity.vo.EmpAuthorityVo;
import com.sevattal.service.leaves.entity.vo.EmpLoginVo;
import com.sevattal.service.leaves.entity.vo.EmpVo;
import com.sevattal.service.leaves.mapper.DeptMapper;
import com.sevattal.service.leaves.mapper.EmpAuthorityMapper;
import com.sevattal.service.leaves.mapper.EmpMapper;
import com.sevattal.service.leaves.service.EmpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private EmpAuthorityMapper empAuthorityMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public boolean createEmp(Emp emp) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_eId", emp.getFEid()).or().eq("f_eUsername",emp.getFEusername());
        int count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        if(emp.getFEpassword() == null){
            emp.setFEpassword("123456");
        }
        emp.setFEpassword(MD5.encrypt(emp.getFEpassword()));
        int result = baseMapper.insert(emp);
        if (result == 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean deleteEmp(String id) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_eId",id);
        int count = baseMapper.delete(queryWrapper);
        if (count == 1 ){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean updateEmp(Emp emp) {
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_eId", emp.getFEid());
        int count = baseMapper.selectCount(queryWrapper);
        if(count == 0) {
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        baseMapper.update(emp,queryWrapper);
        return true;
    }

    @Override
    public EmpVo getByEmpId(String id) {

        EmpVo empvo = baseMapper.getByEmpId(id);

        return empvo;
    }

    /*
    * 注意使用querywrapper 去查询mapper xml中定义的SQL，
    * 不能使用别名作为查询条件的列名
    * */
    @Override
    public List<EmpVo> getEmptList() {
        QueryWrapper<EmpVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("e.f_eId");
        queryWrapper.notIn("e.f_eId","1");
        List<EmpVo> empVoList = baseMapper.getEmpList(queryWrapper);
        return empVoList;
    }

    @Override
    public String login(EmpLoginVo emp) {
        String username = emp.getUsername();
        String password = emp.getPassword();

        /*
        * 验证密码和用户名是否存在
        * */
        if(StringUtils.isEmpty(username)){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(password)){
            throw new SevattalException(ResultCodeEnum.PARAM_ERROR);
        }

        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("f_eUsername",username);
        Emp emp_temp = baseMapper.selectOne(queryWrapper);
        System.out.println(emp_temp.toString());
        // 效验密码是否正确
        System.out.println(MD5.encrypt(password));
        if (!MD5.encrypt(password).equals(emp_temp.getFEpassword())){
            throw new SevattalException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
        }
        // 获取权限
        List<EmpAuthorityVo> empAuthorityVoList = empAuthorityMapper.selectByEid(username);
        List<String> roles = new ArrayList<>();
        for (int i=0; i< empAuthorityVoList.size();i++){
            roles.add(empAuthorityVoList.get(i).getFAname());
        }

        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setId(emp_temp.getFEid());
        jwtInfo.setName(emp_temp.getFEname());
        jwtInfo.setRoles(roles);
        jwtInfo.setAvatar("");
        // 设定令牌有效期为3600秒
        String jwToken = JwtUtils.getJwtToken(jwtInfo, 3600);
        return jwToken;
    }

    @Override
    public List<EmpVo> getListBySearch(EmpVo empVo) {
        List<DeptVo> deptList = deptMapper.getDeptList();
        QueryWrapper<EmpVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("e.f_eId");
        queryWrapper.notIn("e.f_eId","1");
        if (empVo.getFEid() != null && !StringUtils.isEmpty(empVo.getFEid())){
            queryWrapper.eq("e.f_eId", empVo.getFEid());
        }
        if (empVo.getFEname() != null && !StringUtils.isEmpty(empVo.getFEname())){
            queryWrapper.like("e.f_eName",empVo.getFEname());
        }
        if (empVo.getFDid() != null && !StringUtils.isEmpty(empVo.getFDid())){
            queryWrapper.eq("e.f_dId",empVo.getFDid());
        }
        if (empVo.getFEtitle() != null && !StringUtils.isEmpty(empVo.getFEtitle())){
            queryWrapper.eq("e.f_eTitle",empVo.getFEtitle());
        }
        List<EmpVo> empList = baseMapper.getEmpList(queryWrapper);
        return empList;
    }
}
